// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   UserServiceImp.java

package com.iphaeton.lms.business.organize;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.organize.User;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dom4j.*;

// Referenced classes of package com.iphaeton.lms.business.organize:
//            IUserService

public class UserServiceImp extends PersistentServiceSupport
    implements IUserService
{

    public UserServiceImp()
    {
    }

    public User save(User user)
        throws BusinessException
    {
        if(user == null)
            throw new BusinessException("null user isn't allowed in function save(User user)!");
        User u = null;
        try
        {
            u = (User)hbpservice.store(user);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't save user '" + user.getName() + "'!");
        }
        return u;
    }

    public void remove(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function remove(String id)!");
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.organize.User.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove user where id='" + id + "'!");
        }
    }

    public void remove(User u)
        throws BusinessException
    {
        try
        {
            String sql = "delete from t_user where id='" + u.getId() + "'";
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u7528\u6237\u51FA\u9519!");
        }
    }

    public User findUserById(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function findUserById(String id)!");
        User user = null;
        try
        {
            user = (User)hbpservice.get(id, com.iphaeton.lms.pojo.organize.User.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find user where id='" + id + "'!");
        }
        return user;
    }

    public User findUserByUserId(String userId)
        throws BusinessException
    {
        if(userId == null)
            throw new BusinessException("\u7528\u6237ID\u4E0D\u80FD\u4E3A\u7A7A!");
        User user = null;
        String hql = "from User u where u.userId='" + userId + "' order by u.name";
        try
        {
            logger.debug(hbpservice);
            List u = hbpservice.query(hql);
            if(u != null && u.size() == 1)
                user = (User)u.get(0);
            else
                throw new BusinessException("\u7CFB\u7EDF\u4E2D\u627E\u4E0D\u5230\u7528\u6237[" + userId + "]!");
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7CFB\u7EDF\u4E2D\u627E\u4E0D\u5230\u7528\u6237[" + userId + "]!");
        }
        return user;
    }

    public User doLogin(String userId, String password)
        throws BusinessException
    {
        if(userId == null)
            throw new BusinessException("null userId isn't allowed in function doLogin()!");
        User user = null;
        user = findUserByUserId(userId);
        logger.info(user.getPassword());
        logger.info(password);
        logger.info((new StringBuffer(String.valueOf(user.getState()))).toString());
        if(user.getPassword().equals(password) && user.getState() == 'a')
        {
            user.setvalidUser(true);
            return user;
        } else
        {
            return null;
        }
    }

    public String findSysRole(User user)
        throws BusinessException
    {
        if(user == null)
            throw new BusinessException("null id isn't allowed in findRole(String id)!");
        List roleList = null;
        String role = "";
        try
        {
            for(int i = 1; i < 5; i++)
            {
                String hql = "select distinct(p.sort) from Privilege p where p.sort='" + i + "' and p.role.id in (select r.id from Role r where r.user.id='" + user.getId() + "')";
                roleList = hbpservice.query(hql);
                if(roleList != null && roleList.size() > 0)
                    role = role + "1";
                else
                    role = role + "0";
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
        }
        logger.debug("user role list size:" + roleList.size());
        return role;
    }

    public List findGroup(User user, String type)
        throws BusinessException
    {
        if(user == null)
            throw new BusinessException("null user isn't allowed in findGroup(User user, String type)!");
        List groupList = null;
        String hql = "from Group g where g.user.id='" + user.getId() + "' and g.type='" + type + "' order by g.name";
        try
        {
            groupList = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
        }
        logger.debug("user group list size:" + groupList.size());
        return groupList;
    }

    public Document getUserMenuDocument(String user_id, String role)
        throws BusinessException
    {
        List privilegeList = null;
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        privilegeList = findUserPrivilege(user_id, Integer.parseInt(role));
        for(int i = 0; i < privilegeList.size(); i++)
        {
            String id = (String)((Map)privilegeList.get(i)).get("id");
            String name = (String)((Map)privilegeList.get(i)).get("name");
            Element privilegeElement = root.addElement(name);
            List menuList = findPrivilegeMenu(id);
            for(int j = 0; j < menuList.size(); j++)
            {
                Element menuElement = privilegeElement.addElement("menu");
                String mname = (String)((Map)menuList.get(j)).get("name");
                String action = (String)((Map)menuList.get(j)).get("action");
                String image = (String)((Map)menuList.get(j)).get("image");
                menuElement.addAttribute("name", mname);
                menuElement.addAttribute("action", action);
                menuElement.addAttribute("image", image);
            }

        }

        logger.info(doc.toString());
        return doc;
    }

    private List findUserPrivilege(String user_id, int sort)
        throws BusinessException
    {
        List list = null;
        String sql = "select distinct(p.id),p.name from t_privilege p inner join t_role_privilege rp on p.id=rp.privilege_id and p.sort=" + sort + " inner join t_user_role ur on ur.id='" + user_id + "' and ur.role_id=rp.role_id order by p.id";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u7ED9\u5B9A\u89D2\u8272\u6743\u9650\u5217\u8868\u51FA\u9519!");
        }
        return list;
    }

    private List findPrivilegeMenu(String id)
        throws BusinessException
    {
        List list = null;
        String sql = "select m.name as name,m.action as action,m.image as image from t_menu m where m.privilege_id='" + id + "'";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7ED9\u5B9A\u6743\u9650\u83DC\u5355\u5217\u8868\u51FA\u9519!");
        }
        return list;
    }

    private static Logger logger;

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.organize.UserServiceImp.class.getName());
    }
}
